<?php

namespace Admin\Controller;

class TeacherController extends AdminController {
    
    
    /*
     * 老师列表
     */
    public function teacherlist(){
        
        $where = "  user.type = 1 ";
        $url = '';
        
        //页码
        if(I('get.page')){
            $page = I('get.page');
        }else{
            $page = 1;
        }
        
        //姓名搜索
        if(I('get.name')){
            $filter['name'] = (int)I('get.name');
            $url .= '&name=' . $filter['name'];
            $where .= " AND ut.relname  LIKE '%" . $filter['relname']."%'";
        }else{
            $filter['name'] = '';
        }  
        
        //电话搜索
        if(I('get.phone')){
            $filter['phone'] = I('get.phone');
            $url .= '&phone=' . $filter['phone'];
            $where .= " AND user.phone=" . $filter['phone'];
        }else{
            $filter['phone'] = '';
        }        
        
        
        $limit = 8;
        $start = ($page - 1) * $limit; 
        
    	$model = M("user"); 
        $data = $model->join('LEFT JOIN  user_teacher AS ut ON  user.id = ut.user_id')
            ->where($where)->order('user.status,user.addtime desc')->limit($start,$limit)
            ->field('ut.*, user.phone, user.id as new_user_id,   user.status, user.is_certified, user.addtime')->select();

        if($data){
            foreach ( $data as $k => $v){
                if($v['grade']){
                    $data[$k]['grade'] = $this->lesson(explode(",", $v['grade']),'grade');
                }
                if($v['lesson']){
                    $data[$k]['lesson'] = $this->lesson(explode(",", $v['lesson']),'lesson');
                }
                if($v['school']){
                     $data[$k]['school'] = $this->lesson($v['school'],'school');
                }
            }
        }
        $total = $model->join('LEFT JOIN  user_teacher AS ut ON  user.id = ut.user_id')->where($where)->count();
        $pagination = new \Pub\Controller\PaginationController();
        $pagination->total = $total;
        $pagination->page = $page;
        $pagination->limit = $limit;
        $pagination->url = U('Teacher/teacherList') . '?page={page}' . $url;
        
        $render = $pagination->render();
        $this->assign('nav',"teacher");
        $this->assign('data',$data);
        $this->assign('render',$render);
        $this->assign('filter',$filter);
        $this -> display('list');
    }

    /*
     * 老师详情
     */
    public function detail(){
        $id = I('get.id');
    	$data = M('user_teacher')->join('LEFT JOIN  user as u ON u.id = user_teacher.user_id')->where(array('user_id' => $id, 'type' => 1))->find();
        if($data){
            
            if($data['grade']){
                $data['grade'] = $this->lesson(explode(",", $data['grade']),'grade');
            }
            if($data['lesson']){
                $data['lesson'] = $this->lesson(explode(",", $data['lesson']),'lesson');
            }
            if($data['school']){
                 $data['school'] = $this->lesson($data['school'],'school');
            }
            $data['avatar'] = htmlentities($data['avatar']);

        };

        $date = M('user_date')->where(array('user_id' => $id))->select();
        $this->assign('data', $data);
        $this->assign('date', $date);

        $orderlog_comment = M('orderlog_comment');
        $map['teacher_id'] = $id;
        //评论分平均分
        $total = $orderlog_comment->where($map)->avg('total');
        $this->assign('total',(int)$total);
        $accuracy = $orderlog_comment->where($map)->avg('accuracy');
        $this->assign('accuracy',(int)$accuracy);
        $enrich = $orderlog_comment->where($map)->avg('enrich');
        $this->assign('enrich',(int)$enrich);
        $easy = $orderlog_comment->where($map)->avg('easy');
        $this->assign('easy',(int)$easy);
        $experience = $orderlog_comment->where($map)->avg('experience');
        $this->assign('experience',(int)$experience);
        $fast = $orderlog_comment->where($map)->avg('fast');
        $this->assign('fast',(int)$fast);
        //评论类表
        $commentlist =$orderlog_comment->where($map)->order('comment_id desc')->field('*')->select();
        $this->assign('lists',$commentlist);

        $this -> display('detail');
    }
    
    /*
     * 老师审核及认证
     * type:1认证， 2：审核
     */
    public function verify(){
        $result = array(
            'flag' => '0',
            'content' => '操作失败'
        );
        if(IS_POST){
            $user = M('user');
            if(I('post.type') == 1 && I('post.user_id')){
                $data['id'] = I('post.user_id');
                $data['is_certified'] = I('post.is_certified');
                $flag = $user->save($data);
                if($flag){
                    $result = array(
                        'flag' => '1',
                        'content' => '操作成功'
                    );
                }
            }elseif(I('post.type') == 2 && I('post.user_id')){
                $data['id'] = I('post.user_id');
                $data['status'] = I('post.status');
                $flag = $user->save($data);
                if($flag){
                    $result = array(
                        'flag' => '1',
                        'content' => '操作成功'
                    );
                }
            }
        }
        $this->ajaxReturn($result);
        
    }
    
    public  function star(){
        $result = array(
            'flag' => '0',
            'content' => '操作失败'
        );
        if(IS_POST && I('post.user_id')){
            $teacher = M('user_teacher');
            $flag = $teacher->where(array('user_id' => I('post.user_id')))->save(array('star' => I('post.star')));
            if($flag){
                $result = array(
                    'flag' => '1',
                    'content' => '操作成功'
                );
            }            
        }
        $this->ajaxReturn($result);
    }
    
    /*
     * 推荐老师列表
     */
    public function introduction(){
        
        $model = M('recommendation');
        
        //页码
        if (I('get.page')) {
            $page = I('get.page');
        } else {
            $page = 1;
        }
        
        $limit = 8;
        $start = ($page - 1) * $limit;
        $data = $model->join(' left join user u ON u.id = recommendation.user_id')->order('recommendation.id desc')
                ->limit($start, $limit)->field('u.phone AS user_phone , recommendation.*')->select();
        $total = $model->join('user u ON u.id = recommendation.user_id')->count();
        $pagination = new \Pub\Controller\PaginationController();
        $pagination->total = $total;
        $pagination->page = $page;
        $pagination->limit = $limit;
        $pagination->url = U('Teacher/introduction') . '?page={page}';
        $render = $pagination->render();
        $this->assign('nav','rcmd');
        $this->assign('data',$data);
        $this->display('introduction');
    }
    
    /*
     * 处理推荐状态
     */
    public function dealIntroduction(){
        $result = array(
            'flag' => '0',
            'content' => '操作失败'
        );
        if(IS_POST && I('post.')){
            $teacher = M('recommendation');
            $data = I('post.');
            $flag = $teacher->save($data);
            if($flag){
                $result = array(
                    'flag' => '1',
                    'content' => '操作成功'
                );
            }            
        }
        $this->ajaxReturn($result);
    }

    /**
     * 获取老师信息
     */
    public function info(){
        $teacher_id = I("post.teacher_id");
        $user_teacher = M("user_teacher");
        //0.验证老师id是否正确
        $user = M("user");
        $utmap['id'] = $teacher_id;
        $utmap['type'] = 1;
        $rs = $user->where($utmap)->count();
        if($rs!=1){
            $data['error'] = 1;
            $data['msg'] = '该用户不是老师';
            echo json_encode($data);die();
        }
        //1.获取老师信息
        $map['user_id'] = $teacher_id;
        $rs = $user_teacher->join('LEFT JOIN user u ON u.id=user_teacher.user_id')
            ->where($map)->field("user_teacher.relname as name,u.phone")->find();
        if($rs){
            $data['error'] = 0;
            $data['rs'] = $rs;
        }else{
            $data['error'] = 1;
            $data['msg'] = "获取老师信息失败";
        }
        echo json_encode($data);
    }

    /**
     *给老师发工资
     */
    public function paySalary(){
        $teacher_id = I("post.teacher_id");
        $reducesalary = I("post.reducesalary");
        //1.扣除老师工资
        $user_teacher = M("user_teacher");
        $map['user_id'] = $teacher_id;
        $rs = $user_teacher->where($map)->setDec('salary',$reducesalary);
        if(!$rs){
            $data['error'] = 1;
            $data['msg'] = "无法扣除老师工资";
            echo json_encode($data);die();
        }
        //2.生成资金日志
        $orderlog = M("orderlog");
        $data['money'] = $reducesalary;
        $data['addtime'] = date("Y-m-d H:i:s");
        $data['remark'] = "发工资：".$reducesalary."元整";
        $data['user_id'] = $teacher_id;
        $res = $orderlog->add($data);
        $sql = $orderlog->getLastSql();
        if(!$res){
            $data['error'] = 1;
            $data['msg'] = "无法扣除老师工资";
            echo json_encode($data);die();
        }else{
            $data['error'] = 0;
            $data['msg'] = "扣除老师工资成功";
            echo json_encode($data);die();
        }
    }

}
